package com.example.myandroidlibrary;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.provider.Settings;
import android.util.Log;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileFilter;
import java.io.FileReader;
import java.io.IOException;
import java.util.regex.Pattern;

/**
 * @author wdl
 */
public class AndroidSystemHelper {
    /**
     * 获取Android的id,这是一个16位的十六进制数
     *
     * @param context
     * @return
     */
    public static String getAndroidId(Context context) {
        String androidID = Settings.System.getString(context.getContentResolver(),
                Settings.Secure.ANDROID_ID);
        return androidID;
    }

    /**
     * 安装apk文件
     *
     * @param context
     * @param apkFile
     */
    public static void installApk(Context context, File apkFile) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setDataAndType(Uri.fromFile(apkFile),
                "application/vnd.android.package-archive");
        context.startActivity(intent);

    }

    /**
     * 获取本机CPU核心数
     *
     * @return
     */
    public static int getNumCores() {
        /**
         *  Private Class to display only CPU devices in the directory listing
          */

        class CpuFilter implements FileFilter {

            @Override
            public boolean accept(File pathname) {
                // Check if filename is "cpu", followed by a single digit number
                if (Pattern.matches("cpu[0-9]", pathname.getName())) {
                    return true;
                }
                return false;
            }
        }

        try {
            // Get directory containing CPU info
            File dir = new File("/sys/devices/system/cpu/");
            // Filter to only list the devices we care about
            File[] files = dir.listFiles(new CpuFilter());

            // Return the number of cores (virtual CPU devices)
            return files.length;
        } catch (Exception e) {
            // Print exception

            e.printStackTrace();
            // Default to return 1 core
            return 1;
        }
    }

    /**
     * 获取本机物理内存总数（以KB为单位）
     *
     * @return
     */
    public static long getTotalMemory() {
        {
            String str1 = "/proc/meminfo";
            String str2 = null;
            String[] arrayOfString;
            long initial_memory = 0;
            try {
                FileReader localFileReader = new FileReader(str1);
                BufferedReader localBufferedReader = new BufferedReader(
                        localFileReader, 8192);
                try {
                    /* meminfo*/
                    str2 = localBufferedReader.readLine();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                arrayOfString = str2.split("\\s+");
                for (String num : arrayOfString) {
                    Log.i(str2, num + "\t");
                }
                // total Memory
                initial_memory = Integer.valueOf(arrayOfString[1]).intValue();
                localBufferedReader.close();
                return initial_memory;
            } catch (IOException e) {
                return -1;
            }
        }
    }
}
